<template>
  <div class="scrollDiv">
    <div class="login-main">
      <headerBack></headerBack>
      <scroller>
        <div class="loginWrap">
          <h2 class="h2">账号密码登录</h2>
          <h5 class="h5">请输入您手机号的密码进行登录</h5>
          <div class="boxloginWrap">
            <div>
              <div class="login2">
                <xInput
                  plvalue="请输入登录手机号"
                  groupTitle="手机号"
                  v-model="phoneNum"
                  :showXx="showxx"
                  v-on:clears="clear"
                  type="txt"
                ></xInput>
                <xInput
                  plvalue="请输入密码(6-16位)"
                  groupTitle="密码"
                  v-model="pwdNum"
                  :showXx="showxx"
                  type="pwd"
                  v-on:clears="clear"
                ></xInput>
              </div>
              <div>
                <span class="login-desc">
                  <i>注册即代表你同意</i>
                  <b @click="commonJs.gopush('xieyi')">《用户注册协议》</b>
                  <i>和</i>
                  <b @click="commonJs.gopush('yinsi')">《隐私政策》</b>
                </span>
                <sumbit-btn
                  :loading="loadings"
                  message="登录"
                  :isSumbit="showColor"
                  @btnClick="suresLogin('one')"
                ></sumbit-btn>
                <span class="login-other" @click="goMsgLogin">
                  <i>手机快捷登录/注册</i>
                  <i class="right-img"></i>
                </span>
                <wx-login></wx-login>
              </div>
              <div style="height:2rem"></div>
            </div>
          </div>
        </div>
      </scroller>
    </div>
  </div>
</template>

<script>
import JSEncrypt from "jsencrypt/bin/jsencrypt";
import xInput from "~/components/xInput";
import sumbitBtn from "./components/sumbitBtn";
import wxLogin from "./components/wxLogin";
import { identityLogin } from "~/api/identity";
import headerBack from "~/components/headerBack";
import ls from "store2";
import { setInterval, clearInterval } from "timers";
import { authLogin } from "~/common/common";
export default {
  head: {},
  components: {
    xInput,
    sumbitBtn,
    wxLogin,
    headerBack
  },

  data() {
    return {
      loadings: false,
      verifyType: 1,
      phoneNum: "",
      pwdNum: "",
      showxx: false,
      showColor: false,
      gourl: ""
    };
  },
  watch: {
    phoneNum(e) {
      const reg = /^1[3456789]\d{9}$/;
      if (reg.test(e)) {
        this.showyzm = true;
        if (this.pwdNum.length > 5 && this.pwdNum.length < 17) {
          this.showColor = true;
        } else {
          this.showColor = false;
        }
      } else {
        this.showColor = false;
      }
    },
    pwdNum(e) {
      const reg = /^1[3456789]\d{9}$/;
      if (e.length > 5 && reg.test(this.phoneNum) && e.length < 17) {
        this.showColor = true;
      } else {
        this.showColor = false;
      }
    }
  },
  mounted() {
    this.$visit(this.$route.path);
    this.gourl = this.$route.query.returnMall
  },
  methods: {
    goMsgLogin(){
      if(this.$route.query.returnMall){
          // this.$router.replace({path:'msgLogin?goMallUrl='+this.commonJs.GetUrlParam("goMallUrl")})
          this.mallGoLogin("msgLogin",this.$route.query.returnMall)
      }else{
          this.commonJs.gosto('msgLogin')
      }
    },
      mallGoLogin(routerName,queryData){
      	this.$router.replace({
					path: routerName,
					query: {
						returnMall: queryData
					}
				})
    },
    clear() {
      this.phoneNum = "";
    },
    suresLogin(type) {
      if (this.showColor === true) {
        let encryptor = new JSEncrypt(); // 新建JSEncrypt对象
        let publicKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCU+sOs2dwS5k10e44LhAwEVDicF2SM9O6/cDdXuDyemjx/RMT/yDJHQ4+jqJOB11nIdeo2lvrD/9oPMePauTjCaXd7AxO75Gzx24skFOb6Rw5Tvjffgsm0ka+/UbK6OWdruPXJDSCrnRskQVhMNRrV6qGLaOCWCQzNwOcz7aCMQIDAQAB`;
        encryptor.setPublicKey(publicKey); // 设置公钥
        this.regTime = new Date().getTime();
        this.rsaPassWord = encryptor.encrypt(this.regTime + this.pwdNum);
        // 对需要加密的数据进行加密
        let loginObj = {
          account: this.phoneNum,
          credentials: this.rsaPassWord,
          loginTime: this.regTime,
          verifyType: 1
        };
        this.authLogin(loginObj, this.gourl);
      } else {
        this.$vux.toast.text("请正确填写登录信息");
      }
    },
    authLogin(obj, uri) {
      this.loadings = true;
      identityLogin(obj)
        .then(res => {
          if (res.code == "0") {
            let token = res.data.accessToken;
            let userData = res.data;
            let uidVoid = res.data.userVoId;
            ls.set("userType", res.data.userType);
            ls.set("tokensyxk", token);
            ls.set("userVoId", uidVoid);
            ls.set("yxkData", userData);
            this.getToken(uri);
            this.$vux.toast.text(res.message);
          } else {
            this.$vux.toast.text(res.data.message);
          }
        })
        .catch(error => {
          this.$vux.toast.text(error);
        })
        .finally(() => {
          this.loadings = false;
        });
    },
    getToken(uri) {
      
      if (uri) {
        uri = decodeURIComponent(uri);
        let urls = uri + "sx_uid=" + ls.get("userVoId") +"&sx_token=" +ls.get("tokensyxk") +"&open_id=" +ls.get("wxopen_id");
        window.location.replace(urls);
      } else {
        let goRoute = sessionStorage.getItem("goesRouter");
        if (goRoute) {
          goRoute = JSON.parse(sessionStorage.getItem("goesRouter"));
          if (goRoute) {
            sessionStorage.clear();
            if (goRoute.category === "user") {
              this.goRoutes("../user");
              // this.goRoutes(goRoute.paths)
            } else if (goRoute.category === "home") {
              this.goRoutes("../home");
              // window.location.replace(goRoute.paths+"?sx_uid="+ls.get('userVoId') +"&sx_token="+ls.get("tokensyxk")+"&open_id="+ls.get("wxopen_id"))
            }else if(goRoute.category==='vip'){
                    this.goRoutes("../vipUserRoute/openVip?isJustIn=true")
             }
            // }else if(goRoute.category==="goServiceUrl"){
            //   let ure = window.location.href;
            //   let urls = encodeURIComponent(ure);
            //   window.location.replace(goRoute.paths+"?tea_return_url=" +urls +"&tea_userData=" +encodeURIComponent(JSON.stringify(ls.get("yxkData"))))
            // }
          } else {
            this.goRoutes("../user");
          }
        } else {
          this.goRoutes("../user");
        }
      }
    },
    goRoutes(routerName) {
      this.$router.replace({
        path: "../" + routerName
      });
    }
  }
};
</script>

<style scoped lang="less">
@errorColor: #ff210a;

.scrollDiv {
  width: 100%;
  height: 100vh;
  background: #fff;
  overflow: hidden;
}

.login-main {
  background: #fff;
  width: 100%;
  height: 100vh;
  .h2 {
    font-size: 0.4rem;
    font-family: PingFangSC;
    font-weight: 600;
    color: rgba(29, 30, 34, 1);
    line-height: 0.4rem;
    text-align: center;
  }
  .h5 {
    font-size: 0.26rem;
    font-family: PingFangSC;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    line-height: 0.4rem;
    text-align: center;
    margin-top: 0.25rem;
  }
}
.loginWrap {
  width: 100%;
  height: auto;
  min-height: 100vh;
  background: #fff;
  padding: 0 0.6rem;
  margin-top: 1.3rem;
  overflow: hidden;

  .boxloginWrap {
    width: 100%;
    height: auto;
    margin: 0.87rem 0 0;
    box-sizing: border-box;

    .login2 {
      width: 100%;
      height: auto;
      box-sizing: border-box;
    }

    .forgetPwd {
      display: block;
      height: 0.3rem;
      line-height: 0.3rem;
      text-align: center;
      color: #999;
      font-size: 0.28rem;
      margin-top: 0.5rem;
    }

    .login2 {
      margin-bottom: 0.5rem;
    }
  }
}
.login-desc {
  display: block;
  margin: 0.3rem auto;
  text-align: center;
  width: 100%;
  i,
  b {
    font-size: 0.26rem;
    font-family: PingFangSC;
    font-weight: 400;

    line-height: 0.4rem;
  }
  i {
    color: rgba(153, 153, 153, 1);
  }
  b {
    color: @assistColor;
  }
}
.login-other {
  display: block;
  margin: 0.3rem auto;
  text-align: center;
  width: 100%;
  i {
    font-size: 0.26rem;
    font-family: PingFangSC;
    font-weight: 400;
    vertical-align: middle;
    line-height: 0.4rem;
    color: rgba(153, 153, 153, 1);
  }
  .right-img {
    display: inline-block;
    vertical-align: middle;
    margin-left: 0.1rem;

    width: 0.4rem;
    height: 0.4rem;
    background: url("~@/static/sprite/icon_sprites_40.png") -0.1rem -1.3rem;
    background-size: 3rem 2.4rem;
  }
}
</style>
